package cn.itcast.controller;

import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;

    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        PageResult pr = memberService.findByPage(currentPage, pageSize, queryString);
        return new Result(true, "查询成功", pr);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Member member) {
        memberService.add(member);
        return new Result(true, "添加成功！");
    }

    @PutMapping("/update")
    public Result update(@RequestBody Member member){
        memberService.update(member);
        return new Result(true, "修改成功！");
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        Member member = memberService.findById(id);
        return new Result(true,"查找成功！",member);
    }

    @PutMapping("/{id}/{status}")
    public Result updateStatus(@PathVariable("id") Integer id,
                               @PathVariable("status") Integer status){
        memberService.updateStatus(id,status);
        return new Result(true, "修改成功！");
    }
}